import json
from CodeUtil import API1206
# 初始化12306验证码工具
api12306=API1206()
# 统一登录函数
class Login(object):
    def __init__(self,commonproperties):
        self.commonproperties=commonproperties
    # 12306登录函数
    def login(self,session):
        # 1、访问登录页
        login_url = 'https://kyfw.12306.cn/otn/login/init'
        session.get(login_url)
        # 2、获取验证码图片
        captcha_img_url = 'https://kyfw.12306.cn/passport/captcha/captcha-image64'
        captcha_img = session.get(captcha_img_url)
        # with open('captcha.png', 'wb') as fn:
        #     fn.write(captcha_img.content)
        content=captcha_img.text
        result=json.loads(content)
        base64=result['image']
        # 3、校验验证码
        check_captcha_url = 'https://kyfw.12306.cn/passport/captcha/captcha-check'
        from_data = {
            'answer': api12306.discernImg(base64),
            'login_site': 'E',
            'rand': 'sjrand'
        }
        check_captcha_res = session.post(check_captcha_url, data=from_data)
        if check_captcha_res.json()['result_code'] == '4':
            print('验证码校验成功！')
            # 4、校验用户名密码
            check_user_url = 'https://kyfw.12306.cn/passport/web/login'
            from_data = {
                'username': self.commonproperties['user_username'],
                'password': self.commonproperties['user_password'],
                'appid': 'otn'
            }
            check_user_res = session.post(check_user_url, data=from_data)
            if check_user_res.json()['result_code'] == 0:
                # 5、登录状态跟进
                check_user_login_url = 'https://kyfw.12306.cn/otn/login/userLogin'
                check_user_login_res = session.post(check_user_login_url, data={'_json_att': ''})
                if len(check_user_login_res.text) > 0:
                    # 6、继续跟进
                    uamtk_url = 'https://kyfw.12306.cn/passport/web/auth/uamtk'
                    uamtk_res = session.post(uamtk_url, data={'appid': 'otn'})
                    if uamtk_res.json()['result_code'] == 0:
                        newapptk = uamtk_res.json()['newapptk']
                        # 7、最后跟进
                        uamauthclient_url = 'https://kyfw.12306.cn/otn/uamauthclient'
                        uamauthclient_res = session.post(uamauthclient_url, data={'tk': newapptk})
                        if uamauthclient_res.json()['result_code'] == 0:
                            print('登录成功！')
                            # 8、访问个人中心
                            my_url = 'https://kyfw.12306.cn/otn/index/initMy12306'
                            my_res = session.get(my_url)
                            return my_res
                        else:
                            print('登录失败，请重试！')
                            return self.login(session)
                else:
                    print('登录失败,请重试！')
                    return self.login(session)
            else:
                print('登录失败,请重试！')
                return self.login(session)
        else:
            print('验证码输入错误,请重试！')
            return self.login(session)